#include "oeregisterpage.h"
#include <QLineEdit>
#include <QPushButton>
#include <QLabel>
#include <QComboBox>
#include <QHBoxLayout>
#include <QVBoxLayout>
#include <QMouseEvent>
#include <QApplication>

#include <OEMessageBox>

#include <auditcore.h>
OERegisterPage::OERegisterPage(QWidget *parent) : QWidget(parent)
{
    bIsMoveing_ = true;
    resize(650,600);
    QVBoxLayout* layout = new QVBoxLayout();
    QHBoxLayout* layout_user_name = new QHBoxLayout();
    QHBoxLayout* layout_user = new QHBoxLayout();
    QHBoxLayout* layout_pswd = new QHBoxLayout();
    QHBoxLayout* layout_confirm_pswd = new QHBoxLayout();
    QHBoxLayout* layout_issue1 = new QHBoxLayout();
    QVBoxLayout* layout_issue1_v = new QVBoxLayout();
    QHBoxLayout* layout_issue2 = new QHBoxLayout();
    QVBoxLayout* layout_issue2_v = new QVBoxLayout();
    QHBoxLayout* layout_btn = new QHBoxLayout();


    QLabel* lab_logo = new QLabel();
    QLabel* lab_user_name = new QLabel(tr("用户姓名:"));
    QLineEdit* edt_user_name = new QLineEdit();
    QLabel* lab_user = new QLabel(tr("账　　号:"));
    QLineEdit* edt_user = new QLineEdit();
    QLabel* lab_pswd = new QLabel(tr("密　　码:"));
    QLineEdit* edt_pswd = new QLineEdit();
    QLabel* lab_confirm_pswd = new QLabel(tr("确认密码:"));
    QLineEdit* edt_confirm_pswd = new QLineEdit();
    QLabel* lab_issue1 = new QLabel(tr("密　保 1:"));
    QComboBox* box_issue1 = new QComboBox();
    QLineEdit* edt_issue1 = new QLineEdit();
    QLabel* lab_issue2 = new QLabel(tr("密　保 2:"));
    QComboBox* box_issue2 = new QComboBox();
    QLineEdit* edt_issue2 = new QLineEdit();
    QPushButton* btn_submit = new QPushButton(tr("注　册"));
    QPushButton* btn_reset = new QPushButton(tr("重　设"));


    // 数据属性
    lab_logo->setPixmap(QPixmap(":/lab/logo"));
    QRegExp regQuote("[0-9a-zA-Z]{1,12}");
    QRegExpValidator* quoteValidator = new QRegExpValidator(regQuote, this);

    QRegExp reg_cryptolalia("^[\u4e00-\u9fa50-9a-zA-Z]{0,12}$");
    QRegExpValidator* validator_chinese_english = new QRegExpValidator(reg_cryptolalia, this);



    edt_user_name->setPlaceholderText(tr("请输入用户姓名.."));
    edt_user_name->setValidator(validator_chinese_english);

    edt_user->setPlaceholderText(tr("请输入12位以内的英文或数字.."));
    edt_user->setValidator(quoteValidator);

    edt_pswd->setPlaceholderText(tr("请输入6~12位数字或英文.."));
    edt_pswd->setValidator(quoteValidator);
    edt_pswd->setEchoMode(QLineEdit::EchoMode::Password);

    edt_confirm_pswd->setPlaceholderText(tr("请确认密码.."));
    edt_confirm_pswd->setValidator(quoteValidator);
    edt_confirm_pswd->setEchoMode(QLineEdit::EchoMode::Password);

    box_issue1->addItem(tr("你小学班主任的名字"));
    box_issue1->addItem(tr("你初中班主任的名字"));
    box_issue1->addItem(tr("你高中班主任的名字"));
    box_issue1->addItem(tr("你最好的朋友的名字"));
    box_issue1->addItem(tr("你妈妈的名字"));
    box_issue1->addItem(tr("你出生的城市"));
    box_issue1->addItem(tr("你的名字"));


    box_issue2->addItem(tr("你的最大的兴趣爱好"));
    box_issue2->addItem(tr("你学会的第一道菜"));
    box_issue2->addItem(tr("你养的第一只宠物"));
    box_issue2->addItem(tr("你的第一个老板"));
    box_issue2->addItem(tr("你最喜欢的食物"));
    box_issue2->addItem(tr("你就读的高中"));
    box_issue2->addItem(tr("你就读的大学"));


    edt_issue1->setPlaceholderText(tr("请输入密保1的答案.."));
    edt_issue1->setValidator(validator_chinese_english);
    edt_issue2->setPlaceholderText(tr("请输入密保2的答案.."));
    edt_issue2->setValidator(validator_chinese_english);



    // 大小
    edt_user_name->setFixedSize(250,30);
    edt_user->setFixedSize(250,30);
    edt_pswd->setFixedSize(250,30);
    edt_confirm_pswd->setFixedSize(250,30);
    edt_issue1->setFixedSize(250,30);
    edt_issue2->setFixedSize(250,30);
    box_issue1->setFixedSize(250,30);
    box_issue2->setFixedSize(250,30);

    btn_submit->setFixedSize(100,40);
    btn_reset->setFixedSize(100,40);


    // 布局
    layout_user_name->addStretch(999);
    layout_user_name->addWidget(lab_user_name, 1, Qt::AlignHCenter);
    layout_user_name->addWidget(edt_user_name, 1, Qt::AlignHCenter);
    layout_user_name->addStretch(999);

    layout_user->addStretch(999);
    layout_user->addWidget(lab_user);
    layout_user->addWidget(edt_user);
    layout_user->addStretch(999);

    layout_pswd->addStretch(999);
    layout_pswd->addWidget(lab_pswd);
    layout_pswd->addWidget(edt_pswd);
    layout_pswd->addStretch(999);

    layout_confirm_pswd->addStretch(999);
    layout_confirm_pswd->addWidget(lab_confirm_pswd);
    layout_confirm_pswd->addWidget(edt_confirm_pswd);
    layout_confirm_pswd->addStretch(999);

    layout_issue1_v->addWidget(box_issue1);
    layout_issue1_v->addWidget(edt_issue1);
    layout_issue1->addStretch(999);
    layout_issue1->addWidget(lab_issue1, 0, Qt::AlignTop);
    layout_issue1->addLayout(layout_issue1_v);
    layout_issue1->addStretch(999);

    layout_issue2_v->addWidget(box_issue2);
    layout_issue2_v->addWidget(edt_issue2);
    layout_issue2->addStretch(999);
    layout_issue2->addWidget(lab_issue2, 0, Qt::AlignTop | Qt::AlignCenter);
    layout_issue2->addLayout(layout_issue2_v);
    layout_issue2->addStretch(999);

    layout_btn->addStretch(999);
    layout_btn->addWidget(btn_submit);
    layout_btn->setSpacing(30);
    layout_btn->addWidget(btn_reset);
    layout_btn->addStretch(999);

    const int& SPACE = 20;

    /// close button begin
    ///
    QPushButton* btnClose_ = new QPushButton("Χ");
    btnClose_->setFixedSize(40,40);
    btnClose_->setObjectName("CloseButton");

    layout->setMargin(0);
    layout->addWidget(btnClose_,0,Qt::AlignRight | Qt::AlignTop);
    ///
    /// close button end



    layout->addStretch(999);
    layout->addWidget(lab_logo,0,Qt::AlignHCenter);
    layout->addSpacing(SPACE);
    layout->addLayout(layout_user_name);
    layout->addSpacing(SPACE);
    layout->addLayout(layout_user);
    layout->addSpacing(SPACE);
    layout->addLayout(layout_pswd);
    layout->addSpacing(SPACE);
    layout->addLayout(layout_confirm_pswd);
    layout->addSpacing(SPACE);
    layout->addLayout(layout_issue1);
    layout->addSpacing(SPACE);
    layout->addLayout(layout_issue2);
    layout->addSpacing(SPACE);
    layout->addLayout(layout_btn);
    layout->addStretch(999);



//    QWidget* wdgtMainWindow_ = new QWidget();


//    wdgtMainWindow_->setLayout(layout);

//    setCentralWidget(wdgtMainWindow_);

    setLayout(layout);






    // link signal
    connect(btnClose_, &QPushButton::clicked,
            this, &OERegisterPage::sigRegisterFinshed);
    connect(btnClose_, &QPushButton::clicked,
            this, &OERegisterPage::close);

    connect(btn_submit,&QPushButton::clicked,
            this, [=]{
        // 注册
        const QString& user_name = edt_user_name->text();
        const QString& user = edt_user->text();
        const QString& pswd = edt_pswd->text();
        const QString& confirm_pswd = edt_confirm_pswd->text();
        const QString& topic1 = box_issue1->currentText();
        const QString& issue1 = edt_issue1->text();
        const QString& topic2 = box_issue2->currentText();
        const QString& issue2 = edt_issue2->text();

        if (user_name.isEmpty()) {
            OEMessageBox::BriefMessage(this, "姓名不能为空");
            edt_user_name->setFocus();
            return;
        }

        if (user.isEmpty()) {
            OEMessageBox::BriefMessage(this, "账号不能为空");
            edt_user->setFocus();
            return;
        }

        if (pswd.length() < 6) {
            OEMessageBox::BriefMessage(this, "密码不能小于6位");
            edt_pswd->setFocus();
            return;
        }

        if (pswd != confirm_pswd) {
            OEMessageBox::BriefMessage(this, "密码不一致");
            edt_confirm_pswd->setFocus();
            return;
        }

        if (issue1.isEmpty()) {
            OEMessageBox::BriefMessage(this, "答案不能为空");
            edt_issue1->setFocus();
            return;
        }
        if (issue2.isEmpty()) {
            OEMessageBox::BriefMessage(this, "答案不能为空");
            edt_issue2->setFocus();
            return;
        }

        int ret = apiRegisterAccount(user_name.toUtf8().data(),
                           user.toUtf8().data(),
                           pswd.toUtf8().data(),
                           topic1.toUtf8().data(),
                           issue1.toUtf8().data(),
                           topic2.toUtf8().data(),
                           issue2.toUtf8().data());
        if (ret != 0) {
            OEMessageBox::BriefMessage(this, "注册失败");
            return;
        }
        emit sigRegisterFinshed();
        close();

    });



    connect(btn_reset, &QPushButton::clicked,
            this, [=] {
        edt_user_name->clear();
        edt_user->clear();
        edt_pswd->clear();
        edt_confirm_pswd->clear();
        edt_issue1->clear();
        edt_issue2->clear();
        box_issue1->setCurrentIndex(0);
        box_issue2->setCurrentIndex(0);
    });


}

//void OERegisterPage::mouseMoveEvent(QMouseEvent *event)
//{
//    if (!bIsMoveing_)
//        return;
//    if (movePosition_.isNull()) {
//        movePosition_ = event->globalPos() - pos();
//    }
//    //(event->buttons() && Qt::LeftButton)按下是左键
//    //鼠标移动事件需要移动窗口，窗口移动到哪里呢？就是要获取鼠标移动中，窗口在整个屏幕的坐标，然后move到这个坐标，怎么获取坐标？
//    //通过事件event->globalPos()知道鼠标坐标，鼠标坐标减去鼠标相对于窗口位置，就是窗口在整个屏幕的坐标
//    if (bMoveing_ && (event->buttons() && Qt::LeftButton)
//        && (event->globalPos()-movePosition_).manhattanLength() > QApplication::startDragDistance())
//    {
//        move(event->globalPos()-movePosition_);
//        movePosition_ = event->globalPos() - pos();
//    }
//}

//void OERegisterPage::mousePressEvent(QMouseEvent *event)
//{

//    if (!bIsMoveing_)
//        return;
//    bMoveing_ = true;
//    //记录下鼠标相对于窗口的位置
//    //event->globalPos()鼠标按下时，鼠标相对于整个屏幕位置
//    //pos() this->pos()鼠标按下时，窗口相对于整个屏幕位置
//    movePosition_ = event->globalPos() - pos();

//}

//void OERegisterPage::mouseReleaseEvent(QMouseEvent *event)
//{
//    if (!bIsMoveing_)
//        return;
//    bMoveing_ = false;
//}

